package com.lbb.rocketmq;

import java.io.UnsupportedEncodingException;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.common.RemotingHelper;
import com.alibaba.rocketmq.remoting.exception.RemotingException;

public class Producer {
	
	
	public static void main(String[] args) {
		
		  DefaultMQProducer producer = new DefaultMQProducer("producer1");
	        producer.setNamesrvAddr("127.0.0.1:9876");
	        producer.setInstanceName("rmq-instance");
//	        producer.setVipChannelEnabled(false);// // 必须设为false否则连接broker10909端口
		 try {
			producer.start();
			
			 for (int i = 0; i < 10; i++) {
		            Message msg = new Message("MessagePlantform" /* Topic */,
		                "TagA" /* Tag */,
		                ("Hello RocketMQ " +
		                    i).getBytes("utf-8") /* Message body */
		            );
		            SendResult sendResult = producer.send(msg);
		            
		            System.out.printf("%s%n", sendResult);
		        }
		} catch (MQClientException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (RemotingException e) {
			e.printStackTrace();
		} catch (MQBrokerException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	    producer.shutdown();

	}

}
